<template>
    <div>
        <!--顶部添加部分-->
        <div>
            <el-input
                    placeholder="职称名称..."
                    size="small"
                    v-model="jobLevel.name"
                    prefix-icon="el-icon-plus"
                    style="width: 300px">
            </el-input>
            <el-select v-model="jobLevel.titleLevel" size="small" style="margin-left: 6px" placeholder="职称等级">
                <el-option
                        v-for="item in titleLevel"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-button type="primary" size="small" style="margin-left: 6px" icon="el-icon-plus" @click="addJobLevel">
                添加
            </el-button>
        </div>
        <!--表格部分-->
        <div>
            <el-table class="PositionsTable"
                      :data="jobLevels"
                      stripe
                      border
                      @selection-change="handleSelectionChange"
                      style="width: 80%">
                <el-table-column
                        type="selection"
                        width="60">
                </el-table-column>
                <el-table-column
                        label="索引"
                        type="index"
                        width="100"
                        :index="indexMethod">
                </el-table-column>
                <el-table-column
                        v-if="false"
                        prop="id"
                        label="id"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="titleLevel"
                        label="职称等级"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        label="是否启用"
                        width="150">
                    <template scope="scope">
                        <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showJobDialog(scope.$index,scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="deleteJobLevel(scope.$index,scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button size="small"
                       type="danger"
                       :disabled="this.multipleSelection.length===0"
                       style="margin-top: 15px"
                       @click="deleteJoblevels">
                批量删除
            </el-button>
        </div>
        <!--编辑职称弹出框-->
        <el-dialog
                title="编辑职称"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag>职称名称</el-tag>
                        </td>
                        <td>
                            <el-input
                                    class="editPosition"
                                    size="small"
                                    style="margin-left: 10px;width: 205px"
                                    v-model="updateJobLevle.name"
                            ></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>职称等级</el-tag>
                        </td>
                        <td>
                            <el-select v-model="updateJobLevle.titleLevel" size="small" style="margin-left: 10px"
                                       placeholder="职称等级">
                                <el-option
                                        v-for="item in titleLevel"
                                        :key="item"
                                        :label="item"
                                        :value="item">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>是否启用</el-tag>
                        </td>
                        <td>
                            <el-switch
                                    style="display: block;margin-left: 10px"
                                    v-model="updateJobLevle.enabled"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    active-text="已启用"
                                    inactive-text="未启用"
                                    active-value=1
                                    inactive-value=0>
                            </el-switch>
                        </td>
                    </tr>
                </table>


            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="updateJob" size="small">确 定</el-button>
             </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "JoblevelManager",
        data() {
            return {
                //弹出框是否展示
                dialogVisible: false,
                //添加职称数据
                jobLevel: {
                    name: '',
                    titleLevel: '',
                },
                titleLevel: [
                    '正高级',
                    '副高级',
                    '中级',
                    '初级',
                    '员级',
                ],
                //多选框数组
                multipleSelection: [],
                //删除职称的id数组
                jobLevels: [],
                //更新职称数据
                updateJobLevle: {
                    name: '',
                    titleLevel: '',
                    enabled: false,
                    id: '',
                }
            }
        },
        methods: {
            /**
             * 表格索引
             *
             */
            indexMethod(index) {
                return index + 1;
            },

            /**
             * 处理多选框
             *
             */
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            /**
             * 获取表格数据
             *
             */
            initJoblevels() {
                this.getRequest('/system/basic/joblevel/getAllJobLevels').then(res => {
                    if (res) {
                        this.jobLevels = res;
                        this.jobLevel.name = '';
                        this.jobLevel.titleLevel = '';
                    }
                })
            },

            /**
             * 添加职称
             *
             */
            addJobLevel() {
                if (this.jobLevel.name && this.jobLevel.titleLevel) {
                    this.postRequest('/system/basic/joblevel/addJobLevel', this.jobLevel).then(res => {
                        if (res) {
                            this.initJoblevels();
                        }
                    })
                } else {
                    this.$message.error("请补全信息")
                }
            },

            /**
             * 删除职称
             */
            deleteJobLevel(index, data) {
                this.$confirm('此操作将永久删除【' + data.name + '】职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/joblevel/deleteJobLevelById/' + data.id).then(res => {
                        if (res) {
                            this.initJoblevels();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },

            /**
             * 批量删除职称
             *
             */
            deleteJoblevels() {
                this.$confirm('此操作将永久删除' + this.multipleSelection.length + '条数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item => {
                        ids += 'ids=' + item.id + '&';
                    });
                    this.deleteRequest('/system/basic/joblevel/deleteJobLevelByIds/' + ids).then(res => {
                        if (res) {
                            this.initJoblevels();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            /**
             * 展示编辑职位弹出框
             */
            showJobDialog(index, data) {
                this.dialogVisible = true;
                this.updateJobLevle.id = data.id;
                this.updateJobLevle.name = data.name;
                this.updateJobLevle.titleLevel = data.titleLevel;
                this.updateJobLevle.enabled = data.enabled;
            },

            /**
             * 更新职称
             */
            updateJob() {
                console.log(this.updateJobLevle);
                this.putRequest('/system/basic/joblevel/updateJobLevel', this.updateJobLevle).then(res => {
                    if (res) {
                        this.initJoblevels();
                        this.dialogVisible = false;
                    }
                })
            }
        },
        mounted() {
            this.initJoblevels();
        }
    }
</script>

<style scoped>

</style>
